package scantask.client.presentationLayer;

import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.view.orbit.OrbitView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ZoomingPanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// The world window of NASA.
	final private WorldWindow _wwd;
	
	public ZoomingPanel(WorldWindow wwd) {
		
		super();
		this.setLayout(null);
		this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		
		this._wwd = wwd;
		
		JLabel zoom = new JLabel();
        zoom.setText("Zoom (KM):");
        zoom.setBounds(10, 10, 100, 25);
        this.add(zoom);
        
        final JTextArea newZoom = new JTextArea();
        newZoom.setBounds(10, 45, 160, 25);
        this.add(newZoom);
        
        JButton go = new JButton("GO");
        go.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String str = newZoom.getText();
				double z;
				try{
					z = Double.parseDouble(str);
					z *= 1000;
					// Set the zoom to the new zoom.
					((OrbitView)ZoomingPanel.this._wwd.getView()).setZoom(z);
					ZoomingPanel.this.update();
				}
				catch (Exception e) {
					JOptionPane.showMessageDialog(
							null, "Zoom must be a positive number",
							"Error  ", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
        go.setBounds(10, 80, 160, 25);
        this.add(go);
        
        JButton showCurrZoom = new JButton("Show current zoom");
        showCurrZoom.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// Get the current zoom from the globe.
				double z = ((OrbitView)ZoomingPanel.this._wwd.getView()).getZoom();
				z /= 1000;
				newZoom.setText(z + "");
			}
        	
        });
        showCurrZoom.setBounds(10, 115, 160, 25);
        this.add(showCurrZoom);
	
	}
	
	// Update Globe.
    private void update()
    {
        this._wwd.redraw();
    }

}
